﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PlatWL.Data;
using System.Data;
using System.Transactions;

namespace PlatWL.Services.ServicesWeb
{
    public class UserManage
    {
        #region 修改用户信息
        public void UpdateUserInfo(ref Msg_Result msg, int Client, pt_user_account user_account, pt_user_account newuser_account)
        {
            if (string.IsNullOrEmpty(newuser_account.NickName) || string.IsNullOrEmpty(newuser_account.Telephone) || string.IsNullOrEmpty(newuser_account.UserImg))
            {
                msg.error = "请选择照片，输入昵称和手机号";
                return;
            }
            if (!Tools.Common.IsTelMobile(newuser_account.Telephone))
            {
                msg.error = "手机号格式错误";
                return;
            }
            //判断手机号是否已存在
            var listchk = new pt_user_accountb().GetEntities(p => p.ID != user_account.ID && p.Telephone == newuser_account.Telephone);
            if (listchk.Count() > 0)
            {
                msg.error = "手机号已使用";
                return;
            }
            user_account.NickName = newuser_account.NickName;
            //user_account.TrueName = newuser_account.TrueName;
            user_account.UserImg = newuser_account.UserImg;
            user_account.Telephone = newuser_account.Telephone;

            //user_account.Attach1 = newuser_account.Attach1;
            //user_account.Attach2 = newuser_account.Attach2;
            //user_account.Attach3 = newuser_account.Attach3;

            new pt_user_accountb().Update(user_account);

        }
        #endregion

        #region 地址管理
        public void GetAddressFirst(ref Msg_Result msg, int Client, pt_user_account user_account)
        {
            string sql = "SELECT a.ID,a.Receiver,a.Telephone,a.Address,a.SQID,h.Title,h.Province,h.City,h.County FROM pt_user_address a left join pt_shequ_header h on a.SQID=h.ID where a.CRUser=" + user_account.ID + " order by a.IsDefault desc,a.CRTime  desc LIMIT 0,1 ";
            msg.result = new pt_user_addressb().GetDTByCommand(sql);
        }

        public void GetAddressList(ref Msg_Result msg, int Client, pt_user_account user_account)
        {
            string sql = "SELECT a.*,h.Title FROM pt_user_address a left join pt_shequ_header h on a.SQID=h.ID where a.CRUser=" + user_account.ID + " order by CRTime  desc ";
            msg.result = new pt_user_addressb().GetDTByCommand(sql);
        }
        public void GetModel(ref Msg_Result msg, int Client, int ID)
        {
            msg.result = new pt_user_addressb().GetEntity(d => d.ID == ID);
        }

        /// <summary>
        /// 添加地址，并默认
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="Client"></param>
        /// <param name="user_account"></param>
        /// <param name="adv"></param>
        public void AddModel(ref Msg_Result msg, int Client, pt_user_account user_account, pt_user_address adv)
        {
            if (string.IsNullOrEmpty(adv.Receiver) || string.IsNullOrEmpty(adv.Telephone) || string.IsNullOrEmpty(adv.Address))
            {
                msg.error = "请输入收货人和手机号和地址";
                return;
            }
            bool result = false;
            if (adv.ID == 0)
            {
                //adv.Client = Client;
                adv.CRUser = user_account.ID;
                adv.CRTime = DateTime.Now;
                result = new pt_user_addressb().Insert(adv);
            }
            else
            {
                result = new pt_user_addressb().Update(adv);
            }
            if (result)
            {
                if (adv.IsDefault == 1)
                {
                    new pt_user_addressb().ExsSql("update pt_user_address set IsDefault=0 where CRUser=" + user_account.ID + " and ID<>" + adv.ID + " ");
                }
            }
            else
            {
                msg.error = "更新失败";
            }
        }
        public void DelModel(ref Msg_Result msg, int Client, pt_user_account user_account, int ID)
        {
            new pt_user_addressb().Delete(p => p.CRUser == user_account.ID && p.ID == ID);
        }

        /// <summary>
        /// 设置默认地址
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="Client"></param>
        /// <param name="user_account"></param>
        /// <param name="ID"></param>
        public void SetDefault(ref Msg_Result msg, int Client, pt_user_account user_account, int ID)
        {
            new pt_user_addressb().ExsSql("update pt_user_address set IsDefault=0 where CRUser=" + user_account.ID + " and ID<>" + ID + " ");
            new pt_user_addressb().ExsSql("update pt_user_address set IsDefault=1 where CRUser=" + user_account.ID + " and ID=" + ID + " ");
        }
        #endregion

 



    }
}
